package indi.kane.demandlist.service.impl;

import indi.kane.demandlist.dao.MemberMapper;
import indi.kane.demandlist.entity.Member;
import indi.kane.demandlist.service.IMemberService;
import indi.kane.demandlist.util.RedisUtil;
import indi.kane.demandlist.vo.ResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: MemberService
 * @Author: kane
 * @Date: 2020-05-27 2:49 下午
 * @Description:
 */
@Service
public class MemberService implements IMemberService {
    @Resource
    private MemberMapper memberMapper;
    @Resource
    private RedisUtil redisUtil;

    @Override
    public ResultVo deleteByPrimaryKey(Integer id) {
        int result = memberMapper.deleteByPrimaryKey(id);
        return ResultVo.success(result);
    }

    @Override
    public ResultVo insert(Member record) {
        int result = memberMapper.insert(record);
        return ResultVo.success(result);
    }

    @Override
    public ResultVo insertSelective(Member record) {
        int result = memberMapper.insertSelective(record);
        redisUtil.lSet("members", record);
        return ResultVo.success(result);
    }

    @Override
    public ResultVo selectByPrimaryKey(Integer id) {
        Member result = memberMapper.selectByPrimaryKey(id);
        return ResultVo.success(result);
    }

    @Override
    public ResultVo updateByPrimaryKeySelective(Member record) {
        int result = memberMapper.updateByPrimaryKeySelective(record);
        return ResultVo.success(result);
    }

    @Override
    public ResultVo updateByPrimaryKey(Member record) {
        int result = memberMapper.updateByPrimaryKey(record);
        return ResultVo.success(result);
    }

    @Override
    public ResultVo loadAll() {
        List<Member> result = (List<Member>)(List) redisUtil.rangeList("members", 0, -1);
        if(result == null){
            result = memberMapper.loadAll();
        }
        return ResultVo.success(result);
    }

    public ResultVo query(String name) {
        List<Member> result = (List<Member>)(List) redisUtil.rangeList("members", 0, -1);
        if(result == null){
            result = (List<Member>)loadAll().getData();
        }
        result = result.stream().filter(item ->item.getName().contains(name)).collect(Collectors.toList());
        return ResultVo.success(result);
    }
}